const domainName = getApp().globalData.domainName
const dataType = 'json'
const header = {'content-type':'application/x-www-form-urlencoded'}
const method = 'POST'
const responseType = 'text'
// 生成顺序数字的数组
const get_range_num=(max)=>{
  var range_num = []
  for(var i=max;i>=0;i--){
    range_num.push(i)
  }
  return range_num
}





const add_submit=(that)=>{
  wx.showModal({
    content: '确定提交吗？',
    showCancel: true,
    title: '提示',
    success: res => {
      if(res.confirm){
        wx.request({
          url: domainName + '/add_stu_status_submit',
          data: {
            status_name:that.data.status_name,
            order_set:that.data.order_set,
          },
          dataType: dataType,
          enableCache: true,
          enableHttp2: true,
          enableQuic: true,
          header: header,
          method: method,
          responseType: responseType,
          timeout: 0,
          success: res => {
            if(res.data.row_id){
              wx.showToast({
                title: '创建完成',
                duration: 2000,
                icon: 'success',
                mask: false,
              })
              page_init(that)
            }
          },
        })
      }
    },
  })
}


  // 加载权限列表
const page_init=(that)=>{
    wx.request({
      url: domainName + '/load_stu_status_list',
      data: {},
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        let stu_status_list = res.data.stu_status_list
        that.setData({
          order_set:'- 请选择 -',
          show_mask:false,
          status_name:'',
          num_list : get_range_num(stu_status_list.length+1),
          stu_status_list:stu_status_list,
        })
      },
    })
  }

  // 设置选项
const set_opt=(that,e)=>{
    let index = e.currentTarget.id
    var status_id = that.data.stu_status_list[index].status_id
    var status_name = that.data.stu_status_list[index].status_name
    var order_set = that.data.stu_status_list[index].order_set
    wx.showActionSheet({
      itemList: ['设置显示顺序','禁用','恢复正常','修改名称'],
      success: res => {
        if(res.tapIndex==0){
          wx.showModal({
            content:order_set && order_set.toString() || '',
            editable: true,
            placeholderText: '请输入：数字',
            showCancel: true,
            title: '设置显示顺序',
            success: res => {
              if(res.confirm){
                let new_order_set = res.content
                if(!(!isNaN(parseFloat(new_order_set)) && isFinite(new_order_set))){
                  wx.showToast({
                    title: '填写错误！',
                    duration: 2000,
                    icon: 'error',
                  })
                  return
                }
                wx.request({
                  url: domainName + '/update_status_orderSet',
                  data: {
                    status_id:status_id,
                    new_order_set:new_order_set,
                  },
                  dataType: dataType,
                  enableCache: true,
                  enableHttp2: true,
                  enableQuic: true,
                  header: header,
                  method: method,
                  responseType: responseType,
                  timeout: 0,
                  success: res => {
                    wx.showToast({
                      title: '修改完成',
                      duration: 1500,
                      icon: 'success',
                    })
                    page_init(that)
                  },
                })
              }
            },
          })
        }else if(res.tapIndex==1){
          wx.showModal({
            content: '确定要禁用该科目“' + status_name + '“吗？',
            showCancel: true,
            title: '警告',
            success: res => {
              if(res.confirm){
                update_stuStatus_status(that,status_id, 2)
              }
            },
          })
        }else if(res.tapIndex==2){
          wx.showModal({
            content: '确定要恢复该科目“' + status_name + '“吗？',
            showCancel: true,
            title: '恢复正常',
            success: res => {
              if(res.confirm){
                update_stuStatus_status(that,status_id, 1)
              }
            },
          })
        }else if(res.tapIndex==3){
          wx.showModal({
            content:status_name || '',
            editable: true,
            placeholderText: '请输入新名称',
            showCancel: true,
            title: '修改状态名称',
            success: res => {
              if(res.confirm){
                let new_name = res.content
                update_name(that,status_id, new_name)
              }
            },
          })
        }
      },
    })

  }



  // 修改状态
const update_stuStatus_status=(that,status_id, new_status)=>{
    wx.request({
      url: domainName + '/update_stuStatus_status',
      data: {
        stu_status_id:status_id,
        new_status:new_status,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        if(res.data.update_res){
          let title = new_status==1?'恢复':new_status==2?'禁用':''
          wx.showToast({
            title: title + '完成！',
            duration: 1500,
            icon: 'success',
            mask: false,
          })


          page_init(that)
          
        }
      },
    })
  }

  // 修改状态名称
const update_name=(that,row_id, new_name)=>{
    wx.request({
      url: domainName + '/update_status_name',
      data: {
        row_id:row_id,
        new_name:new_name,
      },
      dataType: dataType,
      enableCache: true,
      enableHttp2: true,
      enableQuic: true,
      header: header,
      method: method,
      responseType: responseType,
      timeout: 0,
      success: res => {
        if(res.data.update_res){
          wx.showToast({
            title: '修改完成',
            duration: 1500,
            icon: 'success',
          })
          page_init(that)
          that.setData({
            show_mask:false,
            status_name:'',
          })
        }
      },
    })
  }

  module.exports={
    update_stuStatus_status:update_stuStatus_status,
    update_name:update_name,
    add_submit:add_submit,
    page_init:page_init,
    set_opt:set_opt,
  }